﻿using Tsari.Mes.Plc;

namespace Tsari.Mes.Service;

/// <summary>
/// 程序启动类
/// </summary>
public class Startup
{
    /// <summary>
    /// 配置服务，此方法由运行时调用。使用此方法可将服务添加到容器中。
    /// </summary>
    /// <param name="services">指定服务描述符集合的协定</param>
    public void ConfigureServices(IServiceCollection services)
    {
        services.BuildeMicroCloud()
          // 必要加载模块
          .AddPack<Log4NetPack>()
          .AddPack<AutoMapperPack>()
          .AddPack<SignalRPack>()   //注：如果启用消息模块，就不需要单独启用SignalR模块，消息模块依赖SignalR模块，会执行联动启动。
          .AddPack<EndpointsPack>() //已设置为自动启动模块
          .AddPack<MiniProfilerPack>()
          .AddPack<SwaggerPack>()
          .AddPack<AuthDbContextMigrationPack>()
          .AddPack<MesDbContextMigrationPack>()
          .AddPack<AuthenticationPack>()
          .AddPack<FunctionAuthorizationPack>()
          .AddPack<DataAuthorizationPack>()
          .AddPack<MicroCloud.Pack.Message.MessagePack>()
          .AddPack<AuditPack>()
          .AddPack<SystemsPack>()
          .AddPack<MesPlcPack>();
    }

    /// <summary>
    /// 配置，此方法由运行时调用。使用此方法配置HTTP请求管道。
    /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    /// </summary>
    /// <param name="app">用于配置HTTP管道和路由的web应用程序</param>
    public void Configure(WebApplication app)
    {
        // 有关运行应用程序的web宿主环境的信息
        IWebHostEnvironment env = app.Environment;
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();

            app.UseMigrationsEndPoint();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseMiddleware<JsonExceptionHandlerMiddleware>() // 移除异常中间件JsonExceptionHandlerMiddleware，有异常直接返回给前端，由前端捕捉
            .UseDefaultFiles()// Search default.htm|html,index.htm|index.html
            .UseStaticFiles();// Serve files from wwwroot
                              //.UseDirectoryBrowser();// 开启wwwroot目录浏览        
        app.UseMicroCloud();
    }
}
